package com.Liux.blog.service.impl;

import com.Liux.blog.dao.ArticleMapper;
import com.Liux.blog.dao.UserMapper;
import com.Liux.blog.entity.Article;
import com.Liux.blog.entity.ArticleVo;
import com.Liux.blog.entity.User;
import com.Liux.blog.service.ArticleService;
import com.Liux.blog.service.UserService;
import com.Liux.blog.util.PageHelper;
import com.Liux.blog.vo.ResultStatus;
import com.Liux.blog.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.Cookie;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ArticleServiceImpl implements ArticleService {


    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private UserMapper userMapper;


    @Override
    public List<ArticleVo> getArticle() {
        List<ArticleVo> list = articleMapper.getList();
        return list;
    }

    @Override
    public List<Article> getTop6Article() {
        List<Article> articles = articleMapper.Top6Article();
        return articles;
    }

    @Override
    public Article getArticleById(String aid) {
        Example example = new Example(Article.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",aid);
        List<Article> articles = articleMapper.selectByExample(example);
        return articles.get(0);
    }

    @Override
    public ResultVo createArticle(Article article) {
        article.setId(UUID.randomUUID().toString());
        article.setCreateTime(new Date());
        if(article.getUserId() == null || article.getUserId() == ""){
            Example example = new Example(User.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("loginName","admin");
            List<User> users = userMapper.selectByExample(example);
            article.setUserId(users.get(0).getId());
        }
        int i = articleMapper.insert(article);
        if(i>0){
            return new ResultVo(ResultStatus.OK,"success",null);
        }else{
            return new ResultVo(ResultStatus.NO,"fail",null);
        }
    }

    @Override
    public ResultVo updateArticle(String id,String title,String center) {
        int i = articleMapper.updateArticle(id,title,center);
        if(i>0){
            return new ResultVo(ResultStatus.OK,"success",null);
        }else{
            return new ResultVo(ResultStatus.NO,"fail",null);
        }
    }

    @Override
    public ResultVo deleteArticle(String aid) {
        int i = articleMapper.deleteByPrimaryKey(aid);
        if(i>0){
            return new ResultVo(ResultStatus.OK,"删除成功",null);
        }else{
            return new ResultVo(ResultStatus.NO,"fail",null);
        }
    }

    @Override
    public ResultVo ArticleById(String aid) {
        Example example = new Example(Article.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",aid);
        List<Article> articles = articleMapper.selectByExample(example);
       if(articles.size()>0){
           return new ResultVo(ResultStatus.OK,"success",articles.get(0));
       }else{
           return new ResultVo(ResultStatus.OK,"获取失败",null);
       }
    }

    @Override
    public ResultVo ArticleList(int pageNum,int limit) {
        int start = (pageNum - 1)*limit;
        List<ArticleVo> articles = articleMapper.getArticleList(start,limit);

        Example example = new Example(Article.class);
        Example.Criteria criteria = example.createCriteria();
        int count = articleMapper.selectCountByExample(example);

        int pageCount = count % limit==0 ? count/limit : count/limit + 1;

        PageHelper<ArticleVo> articleVoPageHelper = new PageHelper<>(count, pageCount, articles);
        if(articles.size()>0){
            return new ResultVo(ResultStatus.OK,"success",articleVoPageHelper);
        }else{
            return new ResultVo(ResultStatus.OK,"获取失败",null);
        }
    }

}
